博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linq高级查询
阅读量:5963 次
发布时间:2019-06-19

本文共 6940 字,大约阅读时间需要 23 分钟。

模糊查询()包含

Repeater1.DataSource = con.Users.Where(r=>r.nick.Contains(a)).ToList();
Repeater1.DataBind();

根据开头查

Repeater1.DataSource = con.Users.Where(r => r.nick.StartsWith(a)).ToList();
Repeater1.DataBind();

根据结尾查

Repeater1.DataSource = con.Users.Where(r => r.nick.EndsWith(a)).ToList();
Repeater1.DataBind();

查询个数

IEnumerable
uie = con.Users;Repeater1.DataSource = uie;Repeater1.DataBind();Num.Text = uie.Count().ToString();

或者

List
uie = con.Users.ToList();Repeater1.DataSource = uie;Repeater1.DataBind();Num.Text = uie.Count.ToString();

最大值

Num.Text = con.Users.Max(r => r.code).ToString();
Repeater1.DataBind(); 

最小值

Num.Text = con.Users.Min(r => r.code).ToString();
Repeater1.DataBind(); 

平均值:

Num.Text = con.Users.Average(r => r.code).ToString();
Repeater1.DataBind(); 

求和

Num.Text = con.Users.Sum(r => r.code).ToString();
Repeater1.DataBind();

升序

Repeater1.DataSource = con.Users.OrderBy(r=>r.code);
Repeater1.DataBind();

 

降序

Repeater1.DataSource = con.Users.OrderByDescending(r => r.code);
Repeater1.DataBind();

分页

Repeater1.DataSource = con.Users.Skip(5).Take(5);//跳过5个元素取5个元素Repeater1.DataBind();

  实例:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
用户名 密码 昵称 性别 民族 班级 生日 操作
<%#Eval("usename") %> <%#Eval("password") %> <%#Eval("nick") %> " /> <%#Eval("Nationname") %> <%#Eval("Classname") %> <%#Eval("birthdayStr") %>
修改
删除
当前第
界面
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{    int count = 3;    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            using(Data1128DataContext con=new Data1128DataContext())            {                Repeater1.DataSource = con.Users.Take(count);                Repeater1.DataBind();            }        }        prve.Click += prve_Click;        next.Click += next_Click;    }        //上一页    void prve_Click(object sender, EventArgs e)    {        int nowpage = Convert.ToInt32(txt.Text) - 1;        if (nowpage < 1)            return;        txt.Text = nowpage.ToString();        using (Data1128DataContext con = new Data1128DataContext())        {            Repeater1.DataSource = con.Users.Skip((nowpage-1)*count).Take(count);            Repeater1.DataBind();        }    }    //下一页    void next_Click(object sender, EventArgs e)    {        using (Data1128DataContext con = new Data1128DataContext())        {        int nowpage = Convert.ToInt32(txt.Text) + 1;        int maxpage = Convert.ToInt32(Math.Ceiling((con.Users.Count() * 1.0) / count));        if (nowpage > maxpage)            return;        txt.Text = nowpage.ToString();                    Repeater1.DataSource = con.Users.Skip((nowpage - 1) * count).Take(count);            Repeater1.DataBind();        }    }}
后台

 组合查询

//什么都不填,查全部        //根据所填的内容,查询对应的数据绑定到repeater上        using (Data1128DataContext con = new Data1128DataContext())        {            IQueryable
Ui = con.Users.AsQueryable();//查询全部数据 string Nick = TextBox1.Text.Trim(); string Class = TextBox2.Text.Trim(); string Birthday = TextBox3.Text.Trim(); //判断是否需要填充查询条件 if (Nick.Length > 0) { Ui = Ui.Where(r => r.nick.Contains(Nick)); } if (Class.Length > 0) { Ui = Ui.Where(r => r.Class1.classname.Contains(Class)); } if (Birthday.Length > 0) { if (ddl.SelectedValue == "=") Ui = Ui.Where(r => Convert.ToDateTime(r.birthday).Year == Convert.ToInt32(Birthday)); if (ddl.SelectedValue == ">=") Ui = Ui.Where(r => Convert.ToDateTime(r.birthday).Year >= Convert.ToInt32(Birthday)); if (ddl.SelectedValue == "<=") Ui = Ui.Where(r => Convert.ToDateTime(r.birthday).Year <= Convert.ToInt32(Birthday)); } Repeater1.DataSource = Ui; Repeater1.DataBind(); }
方法一:建议使用,清晰明了
using (Data1128DataContext con = new Data1128DataContext())        {            IQueryable
Unick = con.Users.AsQueryable(); IQueryable
Uclass = con.Users.AsQueryable(); IQueryable
Ubir = con.Users.AsQueryable(); string Nick = TextBox1.Text.Trim(); string Class = TextBox2.Text.Trim(); string Birthday = TextBox3.Text.Trim(); if (Nick.Length > 0) { Unick = Unick.Where(r => r.nick.Contains(Nick)); } if (Class.Length > 0) { Uclass = Uclass.Where(r => r.Class1.classname.Contains(Class)); } if (Birthday.Length > 0) { if (ddl.SelectedValue == "=") Ubir = Ubir.Where(r => Convert.ToDateTime(r.birthday).Year == Convert.ToInt32(Birthday)); if (ddl.SelectedValue == ">=") Ubir = Ubir.Where(r => Convert.ToDateTime(r.birthday).Year >= Convert.ToInt32(Birthday)); if (ddl.SelectedValue == "<=") Ubir = Ubir.Where(r => Convert.ToDateTime(r.birthday).Year <= Convert.ToInt32(Birthday)); } //取集合的交集 var Uall = Unick.Intersect(Uclass).Intersect(Ubir); Repeater1.DataSource = Uall; Repeater1.DataBind(); }
方法二:使用集合的交集

 集合的交集,并集,差集,补集(去重)

List
ListA = new List
();List
ListB = new List
();List
ListResult = new List
(); ListResult = ListA.Distinct().ToList();//去重ListResult = ListA.Except(ListB).ToList();//差集ListResult= ListA.Union(ListB).ToList(); //并集ListResult = ListA.Intersect(ListB).ToList();//交集

 

转载于:https://www.cnblogs.com/maxin991025-/p/6381239.html

你可能感兴趣的文章
关于在VS2005中编写DLL遇到 C4251 警告的解决办法
查看>>
提高信息安全意识对网络勒索病毒说不
查看>>
我的友情链接
查看>>
IDE---Python IDE之Eric5在window下的安装
查看>>
基本安装lnmp环境
查看>>
logstash消费阿里云kafka消息
查看>>
Oracle——条件控制语句
查看>>
day-6 and day-7:面向对象
查看>>
CSU Double Shortest Paths 湖南省第十届省赛
查看>>
webgl像机世界
查看>>
php正则怎么使用(最全最细致)
查看>>
javascript数学运算符
查看>>
LC.155. Min Stack(非优化,两个stack 同步 + -)
查看>>
交互设计[3]--点石成金
查看>>
SCCM TP4部署Office2013
查看>>
redis主从配置<转>
查看>>
bootloader功能介绍/时钟初始化设置/串口工作原理/内存工作原理/NandFlash工作原理...
查看>>
利用console控制台调试php代码
查看>>
讲解sed用法入门帖子
查看>>
Linux 内核已支持苹果
查看>>